import React from 'react';
import { connect } from 'dva';
import { Link } from 'dva/router';
import { Flex, WingBlank, WhiteSpace, List, Badge } from 'antd-mobile';
import styles from './styles.less';

const tagStyles = {
  borderRadius: 3,
  color: '#777',
  backgroundColor: 'transparent',
  border: '1px solid #ddd',
  fontSize: 28,
  height: 'auto',
  padding: '5px 25px',
  marginRight: 20,
  marginBottom: 20,
};

const { Item, Item: { Brief } } = List;

const InquiryDetailPage = ({ dispatch, location, detail, list, ...rest }) => {
  window.scrollTo(0, 0);
  let Birth;
  if(list){
     if(list[0].birthday){
      Birth = list[0].birthday.replace(/-/g, "/");
    }
  }
 
  let Agetime= list[0].age;
  let Ages = "", time = new Date(),times = new Date(Birth);
  let newYear = time.getFullYear(), oldYear = times.getFullYear();
  let newMonth = time.getMonth()+1; let oldMonth = parseInt(times.getMonth())+1;
  let newDay = time.getDate(), oldDay = times.getDate();
  let year = newYear - oldYear,Today = newDay - oldDay, month = newMonth - oldMonth; // 天 // 月;
  if(Agetime >= 1){
       Ages = Agetime + '岁'
    }else{
       if(year == 0){
         if(month >= 1){  
               Ages = month+ '月';  
          }else{
            if(month < 1&&Agetime <=0){
               Ages = Today+ '天';
            }
          }
       }else{
          let m1 = 12 - oldMonth;
          let r1 = 31 - oldDay;
          Ages = m1 + newMonth + '月' ;
          if(m1 == 0){
            if (newMonth ==1 ) {
              Ages = r1 + newDay;
              if (Ages>=31) {
                Ages = 1+'月';
              }else{
                Ages += '天';
              }
            }
          }
          
       }
    } 
  // list[0].Ages = Ages;

  const options = [];
  Object.keys(detail).forEach(key => {
    if (/Q\d+$/i.test(key) && detail[key] ) {
      if (detail[key] != "undefined" && detail[key] != "") {
        options.push(...detail[key].split(';'));
      }
      
    }
  });
  return (
    <div>
     <List>
      <Item>
        <span style={{ marginRight: 20 }}>
          {list[0].name}
        </span>
        {list[0].relation ? (<small>({list[0].relation})</small>) : ""}
        <span style={{ margin: '0 30px' }}>
          {list[0].gender == 1? "男":"女"}
        </span>
        <span>{Ages}</span>
        <span style={{ float: 'right'}}>{detail.createTime }</span>
      </Item>
    </List>
      <WhiteSpace />
      <WingBlank size="md">
        <Link to={{
          pathname: `${location.pathname}/sheet`,
          state: {
            title: '问诊单详情',
            id: location.state.id,
          },
        }}>
          <List>
            <Item
              arrow="horizontal">
              {"查看完整问诊单"}
            </Item>
          </List>
        </Link>
        <WhiteSpace />
        <List wrap>
          <Item extra={null}>辨证结果：
            <Brief>
              <div style={{ width: '100%', overflow: 'auto'}}>
                {detail.synResult && detail.synResult.split(';').map((opt, i) =>
                  <Badge key={i} text={opt} style={tagStyles} />
                )}
              </div>
            </Brief>
          </Item>
          <Item extra={null}>辩病结果：
            <Brief>
              <div style={{ width: '100%', overflow: 'auto'}}>
                {detail.disResult && detail.disResult.split(';').map((opt, i) =>
                  <Badge key={i} text={opt} style={tagStyles} />
                )}
              </div>
            </Brief>
          </Item>
          <Item extra={null}>用药建议：
            <Brief>
              <div style={{ width: '100%', overflow: 'auto'}}>
                {detail.medResult && detail.medResult.split(';').map((opt, i) =>
                  <Badge key={i} text={opt} style={tagStyles} />
                )}
              </div>
            </Brief>
          </Item>
          <Item extra={null} wrap>调养建议：
            <Brief>
              {detail.mediSug && detail.mediSug
                .split('\n')
                .filter(segment => segment !== '')
                .map((segment, i) =>
                  <p key={i}>
                    {segment}
                  </p>
              )}
            </Brief>
          </Item>
        </List>
      </WingBlank>
      <WhiteSpace />
    </div>
  );
};

export default connect(({ inquiry }) => ({
  ...inquiry,
}))(InquiryDetailPage);
